#include "page1_widget.h"
#include "user/welcomewidget.h"


Page1_Widget::Page1_Widget(WelcomeWidget *widget)
{
    this->welcomewidget = widget;
    welcome_ui = this->welcomewidget->ui;

    //RED  LED
    connect(welcome_ui->pb_led_ctrl_r,SIGNAL(clicked()),this,SLOT(on_pb_led_ctrl_r_clicked()));     //建立信号槽
    connect(welcome_ui->pb_brightness_dec_r,SIGNAL(clicked()),this,SLOT(on_pb_brightness_dec_r_clicked()));     //建立信号槽
    connect(welcome_ui->pb_brightness_inc_r,SIGNAL(clicked()),this,SLOT(on_pb_brightness_inc_r_clicked()));     //建立信号槽
    connect(welcome_ui->horizontalSlider_brightness_r, SIGNAL(valueChanged(int)), this, SLOT(on_horizontalSlider_brightness_r_valueChanged(int)));
    //connect(welcome_ui->horizontalSlider_brightness_r, SIGNAL(sliderReleased(int)), this, SLOT(on_horizontalSlider_brightness_r_valueChanged(int)));

    welcome_ui->horizontalSlider_brightness_r->setSingleStep(10);

    welcome_ui->label_brightness_r->setText(QString::number(welcome_ui->horizontalSlider_brightness_r->value(),10));
    welcome_ui->pb_led_ctrl_r->setText("OFF");
    welcome_ui->pb_led_ctrl_r->setCheckable(false);

    //GREEN  LED
    connect(welcome_ui->pb_led_ctrl_g,SIGNAL(clicked()),this,SLOT(on_pb_led_ctrl_g_clicked()));     //建立信号槽
    connect(welcome_ui->pb_brightness_dec_g,SIGNAL(clicked()),this,SLOT(on_pb_brightness_dec_g_clicked()));     //建立信号槽
    connect(welcome_ui->pb_brightness_inc_g,SIGNAL(clicked()),this,SLOT(on_pb_brightness_inc_g_clicked()));     //建立信号槽
    connect(welcome_ui->horizontalSlider_brightness_g, SIGNAL(valueChanged(int)), this, SLOT(on_horizontalSlider_brightness_g_valueChanged(int)));

    welcome_ui->horizontalSlider_brightness_g->setSingleStep(10);

    welcome_ui->label_brightness_g->setText(QString::number(welcome_ui->horizontalSlider_brightness_g->value(),10));
    welcome_ui->pb_led_ctrl_g->setText("OFF");
    welcome_ui->pb_led_ctrl_g->setCheckable(false);

    //BLUE  LED
    connect(welcome_ui->pb_led_ctrl_b,SIGNAL(clicked()),this,SLOT(on_pb_led_ctrl_b_clicked()));     //建立信号槽
    connect(welcome_ui->pb_brightness_dec_b,SIGNAL(clicked()),this,SLOT(on_pb_brightness_dec_b_clicked()));     //建立信号槽
    connect(welcome_ui->pb_brightness_inc_b,SIGNAL(clicked()),this,SLOT(on_pb_brightness_inc_b_clicked()));     //建立信号槽
    connect(welcome_ui->horizontalSlider_brightness_b, SIGNAL(valueChanged(int)), this, SLOT(on_horizontalSlider_brightness_b_valueChanged(int)));

    welcome_ui->horizontalSlider_brightness_b->setSingleStep(10);

    welcome_ui->label_brightness_b->setText(QString::number(welcome_ui->horizontalSlider_brightness_b->value(),10));
    welcome_ui->pb_led_ctrl_b->setText("OFF");
    welcome_ui->pb_led_ctrl_b->setCheckable(false);

    connect(welcome_ui->pb_clear_screen,SIGNAL(clicked()),this,SLOT(on_pb_clear_screen_clicked()));     //建立信号槽
    connect(welcome_ui->pb_relay_ctrl,SIGNAL(clicked()),this,SLOT(on_pb_relay_ctrl_clicked()));     //建立信号槽
    connect(welcome_ui->pb_light_ctrl,SIGNAL(clicked()),this,SLOT(on_pb_light_ctrl_clicked()));     //建立信号槽
    //welcome_ui->pb_relay_ctrl->setText("打 开");
    welcome_ui->pb_relay_ctrl->setCheckable(false);
    welcome_ui->pb_light_ctrl->setCheckable(false);

    key_rgb_relay_ctrl = new Key_RGB_Relay(welcomewidget->m_serial_tx_thread,welcomewidget->m_serial_rx_thread);

    connect(this,SIGNAL(signal_write_sp_tx_queue(char*,unsigned int)),welcomewidget->m_serial_tx_thread,SLOT(slot_write_serial_port_tx_queue(char*, unsigned int)));
    connect(welcomewidget->m_serial_rx_thread,SIGNAL(signal_get_dev_info(unsigned char*,unsigned char)),this,SLOT(slot_get_dev_node_info(unsigned char*,unsigned char)));
    connect(welcomewidget->m_serial_rx_thread,SIGNAL(signal_slave_notify(unsigned char*,unsigned char)),this,SLOT(slot_slave_notify_info(unsigned char*,unsigned char)));
    slave_index = 0;
    slave_count = 0;
    //send_get_dev_info_cmd();    //获取从设备类型

    update_timer = new QTimer();

    if(update_timer != nullptr)
    {
        connect(update_timer,SIGNAL(timeout()),this,SLOT(updatetimer_timeout_handler()));
        update_timer->start(10);
    }

}

Page1_Widget::~Page1_Widget()
{
    this->welcomewidget = nullptr;
    this->welcome_ui = nullptr;
    key_rgb_relay_ctrl = nullptr;

}

//定时器超时处理
void Page1_Widget::updatetimer_timeout_handler()
{
    static unsigned int time_update_count = 0;

    ++time_update_count;

    if(time_update_count > 100)  //1 S
    {

    }

    change_all_leds_status();
    change_relay_status();
}

//RED LED
void Page1_Widget::on_pb_led_ctrl_r_clicked()
{
    static int current_brightness = -1;

    if(welcome_ui->pb_led_ctrl_r->isCheckable())
    {
        welcome_ui->horizontalSlider_brightness_r->setValue(current_brightness);

        welcome_ui->pb_led_ctrl_r->setText("OFF");
        welcome_ui->pb_led_ctrl_r->setCheckable(false);
    }
    else
    {
        current_brightness = welcome_ui->horizontalSlider_brightness_r->value();

        welcome_ui->horizontalSlider_brightness_r->setValue(0);

        welcome_ui->pb_led_ctrl_r->setText("ON");
        welcome_ui->pb_led_ctrl_r->setCheckable(true);
    }
}


void Page1_Widget::on_pb_brightness_dec_r_clicked()
{
    welcome_ui->horizontalSlider_brightness_r->setValue(welcome_ui->horizontalSlider_brightness_r->value()-welcome_ui->horizontalSlider_brightness_r->singleStep());
}


void Page1_Widget::on_pb_brightness_inc_r_clicked()
{
    welcome_ui->horizontalSlider_brightness_r->setValue(welcome_ui->horizontalSlider_brightness_r->value()+welcome_ui->horizontalSlider_brightness_r->singleStep());
}


void Page1_Widget::on_horizontalSlider_brightness_r_valueChanged(int value)
{
    welcome_ui->label_brightness_r->setText(QString::number(value,10));
    welcome_ui->pb_led_ctrl_r->setText("OFF");
    welcome_ui->pb_led_ctrl_r->setCheckable(false);

    key_rgb_relay_ctrl->set_r_brightness(slaver_node[slave_index].slaver_dev_id, (unsigned short)value);
}


//GREEN LED
void Page1_Widget::on_pb_led_ctrl_g_clicked()
{
    static int current_brightness = -1;

    if(welcome_ui->pb_led_ctrl_g->isCheckable())
    {
        welcome_ui->horizontalSlider_brightness_g->setValue(current_brightness);

        welcome_ui->pb_led_ctrl_g->setText("OFF");
        welcome_ui->pb_led_ctrl_g->setCheckable(false);
    }
    else
    {
        current_brightness = welcome_ui->horizontalSlider_brightness_g->value();

        welcome_ui->horizontalSlider_brightness_g->setValue(0);

        welcome_ui->pb_led_ctrl_g->setText("ON");
        welcome_ui->pb_led_ctrl_g->setCheckable(true);
    }
}


void Page1_Widget::on_pb_brightness_dec_g_clicked()
{
    welcome_ui->horizontalSlider_brightness_g->setValue(welcome_ui->horizontalSlider_brightness_g->value()-welcome_ui->horizontalSlider_brightness_g->singleStep());
}


void Page1_Widget::on_pb_brightness_inc_g_clicked()
{
    welcome_ui->horizontalSlider_brightness_g->setValue(welcome_ui->horizontalSlider_brightness_g->value()+welcome_ui->horizontalSlider_brightness_g->singleStep());
}


void Page1_Widget::on_horizontalSlider_brightness_g_valueChanged(int value)
{
    welcome_ui->label_brightness_g->setText(QString::number(value,10));
    welcome_ui->pb_led_ctrl_g->setText("OFF");
    welcome_ui->pb_led_ctrl_g->setCheckable(false);

    key_rgb_relay_ctrl->set_g_brightness(slaver_node[slave_index].slaver_dev_id, (unsigned short)value);
}


//BLUE LED
void Page1_Widget::on_pb_led_ctrl_b_clicked()
{
    static int current_brightness = -1;

    if(welcome_ui->pb_led_ctrl_b->isCheckable())
    {
        welcome_ui->horizontalSlider_brightness_b->setValue(current_brightness);

        welcome_ui->pb_led_ctrl_b->setText("OFF");
        welcome_ui->pb_led_ctrl_b->setCheckable(false);
    }
    else
    {
        current_brightness = welcome_ui->horizontalSlider_brightness_b->value();

        welcome_ui->horizontalSlider_brightness_b->setValue(0);

        welcome_ui->pb_led_ctrl_b->setText("ON");
        welcome_ui->pb_led_ctrl_b->setCheckable(true);
    }
}


void Page1_Widget::on_pb_brightness_dec_b_clicked()
{
    welcome_ui->horizontalSlider_brightness_b->setValue(welcome_ui->horizontalSlider_brightness_b->value()-welcome_ui->horizontalSlider_brightness_b->singleStep());
}


void Page1_Widget::on_pb_brightness_inc_b_clicked()
{
    welcome_ui->horizontalSlider_brightness_b->setValue(welcome_ui->horizontalSlider_brightness_b->value()+welcome_ui->horizontalSlider_brightness_b->singleStep());
}


void Page1_Widget::on_horizontalSlider_brightness_b_valueChanged(int value)
{
    welcome_ui->label_brightness_b->setText(QString::number(value,10));
    welcome_ui->pb_led_ctrl_b->setText("OFF");
    welcome_ui->pb_led_ctrl_b->setCheckable(false);

    key_rgb_relay_ctrl->set_b_brightness(slaver_node[slave_index].slaver_dev_id, (unsigned short)value);
}

void Page1_Widget::on_pb_light_ctrl_clicked()
{
    //qDebug() << ">>> on_pb_relay_ctrl_clicked >>>";
    if(welcome_ui->pb_light_ctrl->isCheckable())
    {
        qDebug() << ">>> on_pb_light_ctrl_clicked >>> on >>>";

        welcome_ui->pb_light_ctrl->setCheckable(false);

        emit welcome_ui->pb_led_ctrl_r->clicked(true);
        emit welcome_ui->pb_led_ctrl_g->clicked(true);
        emit welcome_ui->pb_led_ctrl_b->clicked(true);
    }
    else
    {
        qDebug() << ">>> on_pb_light_ctrl_clicked >>> off >>>";

        welcome_ui->pb_light_ctrl->setCheckable(true);

        emit welcome_ui->pb_led_ctrl_r->clicked(false);
        emit welcome_ui->pb_led_ctrl_g->clicked(false);
        emit welcome_ui->pb_led_ctrl_b->clicked(false);
    }
}

void Page1_Widget::slot_pb_led_ctrl_all_clicked(bool flag)
{
    qDebug() << ">>> slot_pb_led_ctrl_all_clicked >>>" << flag;

    all_leds_status = flag;

    //Tuya_IoT_Kit_Qthread::tuya_iot_report_bool_value((char*)DP_LIGHT_CTRL, flag);
}

void Page1_Widget::change_all_leds_status()
{
    static bool last_status = false;

    if(last_status != all_leds_status)
    {
        qDebug() << ">>> change_all_leds_status >>>" << all_leds_status;
        if(all_leds_status)
        {
            emit welcome_ui->pb_led_ctrl_r->clicked(false);
            emit welcome_ui->pb_led_ctrl_g->clicked(false);
            emit welcome_ui->pb_led_ctrl_b->clicked(false);
        }
        else
        {
            emit welcome_ui->pb_led_ctrl_r->clicked(true);
            emit welcome_ui->pb_led_ctrl_g->clicked(true);
            emit welcome_ui->pb_led_ctrl_b->clicked(true);
        }

        last_status = all_leds_status;
    }
}

//RELAY CTRL
void Page1_Widget::on_pb_relay_ctrl_clicked()
{
    //qDebug() << ">>> on_pb_relay_ctrl_clicked >>>";
    if(welcome_ui->pb_relay_ctrl->isCheckable())
    {
        qDebug() << ">>> on_pb_relay_ctrl_clicked >>> on >>>";
        //welcome_ui->pb_relay_ctrl->setText("打 开");
        welcome_ui->pb_relay_ctrl->setCheckable(false);

        key_rgb_relay_ctrl->set_relay_onoff(slaver_node[slave_index].slaver_dev_id, false);
    }
    else
    {
        qDebug() << ">>> on_pb_relay_ctrl_clicked >>> off >>>";
        //welcome_ui->pb_relay_ctrl->setText("关 闭");
        welcome_ui->pb_relay_ctrl->setCheckable(true);

        key_rgb_relay_ctrl->set_relay_onoff(slaver_node[slave_index].slaver_dev_id, true);
    }
}

void Page1_Widget::slot_pb_relay_ctrl_clicked(bool flag)
{
    qDebug() << ">>> slot_pb_relay_ctrl_clicked >>>" << flag;

    //key_rgb_relay_ctrl->set_relay_onoff(slaver_node[slave_index].slaver_dev_id, flag);
    relay_status = flag;
}

void Page1_Widget::change_relay_status()
{
    static bool last_status = false;

    if(last_status != relay_status)
    {
        qDebug() << ">>> change_relay_status >>>" << relay_status;

        //emit welcome_ui->pb_relay_ctrl->clicked();
        key_rgb_relay_ctrl->set_relay_onoff(slaver_node[slave_index].slaver_dev_id, relay_status);
        last_status = relay_status;
    }
}

void Page1_Widget::on_pb_clear_screen_clicked()
{
    welcome_ui->plainTextEdit_data_display->clear();
}

void Page1_Widget::display_msg(QString str)
{
    welcome_ui->plainTextEdit_data_display->appendPlainText(str);
}

void Page1_Widget::send_get_dev_info_cmd(void)
{
    unsigned int data_len = 0;
    TYP_UN_COMMUNICATE_BUFF p_data_send;
    My_Protocol my_protocol;

    unsigned char dev_type[2];

    dev_type[0] = (unsigned char)(key_rgb_relay_ctrl->rgb_relay.dev_type >> 8);
    dev_type[1] = (unsigned char)(key_rgb_relay_ctrl->rgb_relay.dev_type);

    data_len = my_protocol.protocol_data_package(CMD_GET_DEV_INFO,sizeof(dev_type),dev_type,&p_data_send);

    emit signal_write_sp_tx_queue((char*)&p_data_send.databuff,data_len);
}

void Page1_Widget::slot_get_dev_node_info(unsigned char *p_data, unsigned char len)
{
    //qDebug() << ">>> slot_get_dev_node_info >>>";
    QByteArray ba_data((char*)p_data,(int)len);
    QString str_data = ba_data.toHex();
    unsigned short dev_type = 0xFFFF;
    unsigned char i = 0;
    unsigned char start_index = 0;

    qDebug() << str_data << len;

    dev_type = (unsigned short)((p_data[1] << 8) | p_data[2]);
    slave_count = p_data[3];
/*
    typedef struct slave_node
    {
        unsigned short slaver_net_id;
        unsigned short slaver_dev_id;
        unsigned short slaver_channel_id;
        unsigned short slaver_dev_type;
        unsigned short online_flag;
    }slave_node_t;
*/
    if(dev_type == key_rgb_relay_ctrl->rgb_relay.dev_type)
    {
        if(slave_count > 0)
        {
            //memcpy(&slaver_node, &p_data[4],slave_count*sizeof(slave_node_t));
            for(i = 0;i < slave_count;i++)
            {
                start_index = 4+i*sizeof(slave_node_t);

                slaver_node[i].slaver_net_id = (unsigned short)((p_data[start_index] << 8) | p_data[start_index+1]);
                start_index += 2;
                slaver_node[i].slaver_dev_id = (unsigned short)((p_data[start_index] << 8) | p_data[start_index+1]);
                start_index += 2;
                slaver_node[i].slaver_channel_id = (unsigned short)((p_data[start_index] << 8) | p_data[start_index+1]);
                start_index += 2;
                slaver_node[i].slaver_dev_type = (unsigned short)((p_data[start_index] << 8) | p_data[start_index+1]);
                start_index += 2;
                slaver_node[i].online_flag = (unsigned short)((p_data[start_index] << 8) | p_data[start_index+1]);
                start_index += 2;
            }
        }
    }
}

void Page1_Widget::slot_slave_notify_info(unsigned char *p_data, unsigned char len)
{
    qDebug() << ">>> slot_slave_notify_info >>>";

    QByteArray ba_data((char*)p_data,(int)len);
    QString str_data = ba_data.toHex();

    unsigned char flag = 0;
    unsigned char key_value = 0;

    qDebug() << str_data << len;

    flag = p_data[4];
    key_value = p_data[5];

    if(key_value & 0x01)
    {
        if(flag & 0x01)display_msg("用户按键 1 长按");
        else display_msg("用户按键 1 短按");
    }

    if(key_value & 0x02)
    {
        if(flag & 0x02)display_msg("用户按键 2 长按");
        else display_msg("用户按键 2 短按");
    }

    if(key_value & 0x04)
    {
        if(flag & 0x04)display_msg("用户按键 3 长按");
        else display_msg("用户按键 3 短按");
    }
}
